<template>
    <div class="row list-panel">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <ol class="breadcrumb">
                        <li><a>推广管理</a></li>
                        <li><a>广告列表</a></li>
                    </ol>
                </div>
                <div class="content-wrapper">
                    <el-form :inline="true" :model="queryForm" class="demo-form-inline" label-width="80px"
                             size="small">
                        <el-row>
                            <el-form-item label="链接类别">
                                <el-select v-model="queryForm.linkType" placeholder="请选择链接类别">
                                    <el-option label="促销活动" value="1"></el-option>
                                    <el-option label="商品详情" value="2"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item style="float: right;">
                                <a class="revision-btn" href="javaScript:void(0)" @click="onQuery">搜 索</a>&nbsp;&nbsp;
                            </el-form-item>
                        </el-row>
                    </el-form>
                    <div class="col-sm-12" style="padding: 12px 0px 12px 0px;">
                        <div class="alert alert-info" style="margin-bottom: 0px">
                            <a class="revision-btn" href="javaScript:void(0)" @click="openNewModal">新增</a>
                            <a class="revision-btn" href="javaScript:void(0)" @click="batchDeleteRow">批量删除</a>&nbsp;&nbsp;
                        </div>
                    </div>

                    <template v-if="data != null">
                        <el-table
                            ref="dataTable" class="list-table"
                            :border="true"
                            :data="data.list"
                            tooltip-effect="dark"
                            :header-cell-style="{'background-color': '#F5F5F6'}"
                            max-height="600"
                            style="width: 100%">
                            <el-table-column type="selection" align="center"
                                             width="55">
                            </el-table-column>
                            <template v-for="columnName in columnNames">
                                <template v-if="columnName.prop === 'enable'">
                                    <el-table-column align="center"
                                                     :label="columnName.label"
                                                     :prop="columnName.prop">
                                        <template slot-scope="scope">
                                            <span style="color:#F00;"
                                                  v-if="scope.row.enable === 0">未启用</span>
                                            <span style="color:#1ab394;"
                                                  v-if="scope.row.enable === 1">已启用</span>
                                        </template>
                                    </el-table-column>
                                </template>
                                <template v-else-if="columnName.prop === 'pic'">
                                    <el-table-column align="center"
                                                     :label="columnName.label"
                                                     :prop="columnName.prop">
                                        <template slot-scope="scope" v-if="!$isEmptyStr(scope.row.pic)">
                                            <el-popover trigger="hover" placement="top">
                                                <div style="text-align: center;">
                                                    <img :src="scope.row.pic" class="image"
                                                         style="width: 200px;height:200px">
                                                </div>
                                                <div slot="reference" class="name-wrapper">
                                                    <img :src="scope.row.pic" class="image"
                                                         style="width: 36px;height:36px">
                                                </div>
                                            </el-popover>
                                        </template>
                                    </el-table-column>
                                </template>
                                <template v-else-if="columnName.prop === 'link'">
                                    <el-table-column align="center"
                                                     :label="columnName.label"
                                                     :prop="columnName.prop">
                                        <template slot-scope="scope">
                                            <span v-if="$isEmptyStr(scope.row.link)">促销活动</span>
                                            <span v-else>商品详情</span>
                                        </template>
                                    </el-table-column>
                                </template>
                                <template v-else>
                                    <el-table-column align="center"
                                                     :label="columnName.label"
                                                     :prop="columnName.prop">
                                    </el-table-column>
                                </template>
                            </template>
                            <el-table-column align="center"
                                             fixed="right"
                                             label="操作"
                                             width="300">
                                <template slot-scope="scope">
                                    <el-button type="text" @click="editDetail(scope.row.bannerId)" size="small">
                                        修改
                                    </el-button>
                                    <el-button type="text" @click="onDelRow(scope.row.bannerId)" size="small">删除
                                    </el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </template>
                    <el-pagination style="float: right;margin-top: 10px"
                                   @size-change="handleSizeChange"
                                   @current-change="handleCurrentChange"
                                   :current-page.sync="data.currPage"
                                   :page-size="data.pageSize"
                                   layout="total, prev, pager, next"
                                   :total="data.totalCount">
                    </el-pagination>
                </div>
                <add-ad-modal ref="categoryModal" :editId="bannerId" :showAddModal="showAddModal"
                              @onSubmitNewModal="onSubmitNewModal"
                              @onCancelNewModal="onCancelNewModal"></add-ad-modal>
                <confirm-modal :showConfirmModal="showConfirmModal" @closeModal="closeConfirmModal" :isSingle="isSingle"
                               @submitModal="submitBatchDeleteRow"></confirm-modal>
            </div>
        </div>
    </div>
</template>

<script>
    import AddAdModal from './addAdModal.vue';
    import ConfirmModal from "../../components/confirmModal";

    export default {
        name: 'ManagerList',
        components: {ConfirmModal, AddAdModal},
        data() {
            return {
                categoryList: [],
                showAddModal: false,
                showConfirmModal: false,
                isSingle: false,
                queryForm: {
                    linkType: ''
                },
                columnNames: [{
                    label: '图片',
                    prop: 'pic',
                    width: 120
                }, {
                    label: '链接类别',
                    prop: 'link',
                    width: 120
                }, {
                    label: '是否启用',
                    prop: 'enable',
                    width: 120
                }, {
                    label: '创建时间',
                    prop: 'createTime',
                    width: 120
                }],
                multipleSelection: [],
                data: {},
                query: {
                    page: ''
                },
                ids: [],
                bannerId: ''
            }
        },
        mounted: function () {
            this.listAllAds();
        },
        methods: {
            onQuery() {
                this.listAllAds(this.queryForm);
            },
            handleSizeChange(val) {
            },
            handleCurrentChange(val) {
                this.queryForm.page = val;
                this.listAllAds(this.queryForm);
            },
            async listAllAds(queryForm) {
                let res = await this.$api.get('/mall/banner/list', queryForm);
                if (res.code === 0) {
                    this.data = res.page;
                } else {
                    this.$message.error(res.msg);
                }
            },
            onCancelNewModal() {
                this.showAddModal = false;
            },
            onSubmitNewModal() {
                this.showAddModal = false;
                this.listAllAds();
            },
            batchDeleteRow() {
                let rows = this.$refs.dataTable.selection;
                if (rows.length === 0) {
                    this.$message.warning('请选择一条记录');
                    return;
                } else {
                    let ids = [];
                    for (let row of rows) {
                        ids.push(row.bannerId);
                    }
                    this.ids = ids;
                    this.showConfirmModal = true;
                }
            },
            closeConfirmModal() {
                this.showConfirmModal = false;
            },
            async submitBatchDeleteRow() {
                let res = await this.$api.post('/mall/banner/delete', this.ids);
                if (res.code === 0) {
                    this.ids = this.$options.data().ids;
                    this.$message.success('删除成功');
                    this.showConfirmModal = false;
                    this.listAllAds(this.queryForm);
                } else {
                    this.$message.error(res.msg);
                }
            },
            onDelRow(id) {
                this.ids.push(id);
                this.isSingle = true;
                this.showConfirmModal = true;
            },
            openNewModal() {
                this.bannerId = '';
                this.$refs.categoryModal.modalLabel = '新增广告';
                this.showAddModal = true;
            },
            editDetail(id) {
                this.bannerId = new String(id);
                this.$refs.categoryModal.modalLabel = '编辑广告';
                this.$refs.categoryModal.getInfo(id);
                this.showAddModal = true;
            },
        }
    }
</script>

<style scoped>
    .list-panel .content-wrapper /deep/ .el-form-item__label {
        font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        color: #666;
        font-size: 12px;
        font-weight: normal;
    }
    .list-table {
        font-size: 12px;
    }
</style>
